﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebNext.Domain.Interfaces;
using NHibernate;

namespace WebNext.DataAccess.Infrastructure
{
	public class NHibernateContext : IDatabaseContext
	{
		internal ISession Session
		{
			get;
			private set;
		}

		public NHibernateContext(ISession session)
		{
			this.Session = session;
			this.Session.BeginTransaction();
		}

		public void Rollback()
		{
			if (this.Session != null && this.Session.Transaction != null)
			{
				this.Session.Transaction.Rollback();
			}
		}

		public void Dispose()
		{
			if (this.Session != null)
			{
				if (this.Session.Transaction != null && this.Session.Transaction.IsActive)
				{
					this.Session.Transaction.Commit();
				}
				this.Session.Dispose();
				this.Session = null;
			}
		}
	}
}
